package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IAttendReportDAO;
import org.jsola.hr.dto.AttendReportAddDTO;
import org.jsola.hr.dto.AttendReportUpdateDTO;
import org.jsola.hr.entity.AttendReportDO;
import org.jsola.hr.query.AttendReportQuery;
import org.jsola.hr.service.IAttendReportService;
import org.jsola.hr.vo.AttendReportListVO;
import org.jsola.hr.vo.AttendReportVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrAttendReportServiceImpl")
public class AttendReportServiceImpl implements IAttendReportService {

    @Autowired
    private IAttendReportDAO attendReportDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public AttendReportVO save(AttendReportAddDTO attendReportAddDTO, TokenUser tokenUser) {
        // 转DO
        AttendReportDO attendReportDO = attendReportAddDTO.to(AttendReportDO.class);
        // 保存
        attendReportDO = save(attendReportDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return attendReportDO.to(AttendReportVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(AttendReportUpdateDTO attendReportUpdateDTO, TokenUser tokenUser) {
        //转DO
        AttendReportDO attendReportDO = attendReportUpdateDTO.to(AttendReportDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(attendReportDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... attendReportIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) attendReportIds);
    }


    @Override
    public AttendReportVO selectById(String attendReportId, String siteId) {
        AttendReportDO attendReportDO = selectDOById(attendReportId, siteId);
        if (attendReportDO == null) {
            return null;
        }
        return attendReportDO.to(AttendReportVO.class);
    }

    @Override
    public List<AttendReportListVO> select(AttendReportQuery attendReportQuery, String siteId) {
        List<AttendReportDO> attendReportDOList = selectDO(attendReportQuery, siteId);
        if(CollectionUtils.isEmpty(attendReportDOList)) {
            return attendReportDOList == null ? null : new ArrayList<>();
        }
        return attendReportDOList.stream()
                .map(attendReportDO -> attendReportDO.to(AttendReportListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(AttendReportQuery attendReportQuery, String siteId) {
        Example example = buildExample(attendReportQuery,siteId);
        return attendReportDAO.selectCountByExample(example);
    }

    @Override
    public Page<AttendReportListVO> selectPage(AttendReportQuery attendReportQuery, String siteId) {
        Example example = buildExample(attendReportQuery,siteId);
        Page<AttendReportDO> page = attendReportDAO.selectPageByExample(example,
                attendReportQuery.getPageNo(),
                attendReportQuery.getPageSize());

        return page.to(AttendReportListVO.class);
    }

    @Override
    public AttendReportDO selectDOById(String attendReportId, String siteId) {
        return listById(attendReportId, siteId);
    }

    @Override
    public List<AttendReportDO> selectDO(AttendReportQuery attendReportQuery, String siteId) {
        Example example = buildExample(attendReportQuery,siteId);
        return attendReportDAO.selectByExample(example);
    }

    @Override
    public int deleteByAttendPlanId(String attendPlanId, String siteId) {
        AttendReportQuery attendReportQuery = new AttendReportQuery();
        attendReportQuery.setAttendPlanId(attendPlanId);
        Example example = buildExample(attendReportQuery,siteId);
        return attendReportDAO.deleteByExample(example);
    }

    @Override
    public int logicDelByAttendPlanId(String attendPlanId, TokenUser tokenUser) {

        // 查询
        AttendReportQuery attendReportQuery = new AttendReportQuery();
        attendReportQuery.setAttendPlanId(attendPlanId);
        List<AttendReportDO> dataList = this.selectDO(attendReportQuery, tokenUser.getSiteId());
        if (dataList.size() <= 0) {
            return 0;
        }

        for (AttendReportDO attendReport : dataList) {

            AttendReportDO attendReportDO = new AttendReportDO();
            attendReportDO.setId(attendReport.getId());
            attendReportDO.setValid(false);
            attendReportDO.setModifiedUserId(tokenUser.getUserId());
            attendReportDO.setGmtModified(new Date());
            attendReportDAO.updateByPrimaryKeySelective(attendReportDO);
        }

        return 0;
    }


    /**
     * 根据查询参数，构建example

     * @param attendReportQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(AttendReportQuery attendReportQuery, String siteId) {
        Example example = new Example(AttendReportDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (attendReportQuery.getCompanyId() != null) {
            example.and().andLike("companyId", "%" + attendReportQuery.getCompanyId() + "%");
        }
        if (attendReportQuery.getCompanyName() != null) {
            example.and().andLike("companyName", "%" + attendReportQuery.getCompanyName() + "%");
        }
        if (attendReportQuery.getDeptId() != null) {
            example.and().andLike("deptId", "%" + attendReportQuery.getDeptId() + "%");
        }
        if (attendReportQuery.getDeptName() != null) {
            example.and().andLike("deptName", "%" + attendReportQuery.getDeptName() + "%");
        }
        if (attendReportQuery.getAttendPlanId() != null) {
            example.and().andLike("attendPlanId", "%" + attendReportQuery.getAttendPlanId() + "%");
        }
        if (attendReportQuery.getEmpInfoId() != null) {
            example.and().andLike("empInfoId", "%" + attendReportQuery.getEmpInfoId() + "%");
        }
        if (attendReportQuery.getEmpInfoName() != null) {
            example.and().andLike("empInfoName", "%" + attendReportQuery.getEmpInfoName() + "%");
        }
        if (attendReportQuery.getGmtCreateStart() != null &&
                attendReportQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", attendReportQuery.getGmtCreateStart(),
                    attendReportQuery.getGmtCreateEnd());
        }
        if (attendReportQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + attendReportQuery.getCreateUserId() + "%");
        }
        if (attendReportQuery.getGmtModifiedStart() != null &&
                attendReportQuery.getGmtModifiedEnd() != null) {
            example.and().andBetween("gmtModified", attendReportQuery.getGmtModifiedStart(),
                    attendReportQuery.getGmtModifiedEnd());
        }
        if (attendReportQuery.getModifiedUserId() != null) {
            example.and().andLike("modifiedUserId", "%" + attendReportQuery.getModifiedUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example,attendReportQuery.getOrders());
        return example;
    }
}




